﻿using SGArchiveSystem7x.App_Code;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace SGArchiveSystem7x.Controllers.Operation
{
    public class OperationController : Controller
    {
        // GET: Operation
        public ActionResult Index()
        {
            //加载运营grid
            var OperationList = SGArchiveSystem7x.Models.CColumnsModel.Create_OperationColumns();
            ViewBag.OperationList = OperationList;
            var dep = SGArchiveSystem7x.Models.CComboboxModel.Create_ComboboxDepartment(true, false);
            ViewBag.Dep = dep;
            return View();
        }

        public ActionResult UploadIndex(int id)
        {
            ViewBag.ID = id;
            return View();
        }

        //获取运营数据
        [HttpPost]
        public ActionResult Get_ajaxOperationList(string dh, int status, string stime, string etime)
        {
            int count = 0;
            var list = SGArchiveSystem7xDB.Linq.T_OperationDal.Get_Operation(out count, dh, stime, etime, status);
            List<string> list1 = new List<string>(4);
            list1.Add(dh);
            list1.Add(stime);
            list1.Add(etime);
            list1.Add(status.ToString());
            App_Code.CSession.Add_QueryWhere(Session, list1);
            var json = new
            {
                rows = list.ToArray(),
                total = count
            };
            return Json(json, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="num"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Get_ajaxOperationListPager(int num, int size)
        {
            try
            {
                int iStart = (num - 1) * size;
                var listWhere = CSession.Get_QueryWhere(Session);
                int count = 0;
                var list = SGArchiveSystem7xDB.Linq.T_OperationDal.Get_Operation(out count, listWhere[0], listWhere[1], listWhere[2], int.Parse(listWhere[3]), iStart, CWebCommons._iPageRows); ;

                var json = new
                {
                    rows = list.ToArray(),
                    total = count
                };
                return Json(json, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {

                throw;
            }
           
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public string Get_ajaxOperationAdd(string dh, string tm, int dep, string gtime, int status)
        {
            var user = SGArchiveSystem7x.App_Code.CSession.Get_User(Session);
            DateTime st = DateTime.Parse(gtime);
            SGArchiveSystem7xDB.T_YYFILE en = new SGArchiveSystem7xDB.T_YYFILE()
            {
                YY_BH = dh,
                YY_TM = tm,
                YY_USERID = user.U_ID,
                YY_USERNAME = user.U_RNAME,
                YY_DEPID = dep,
                YY_DATE = st,
                YY_STATUS = status
            };
            return SGArchiveSystem7xDB.Linq.T_OperationDal.Add_Operation(en).ToString();
        }

        /// <summary>
        /// 将运营发布文书档案
        /// </summary>
        /// <returns></returns>
        public string Get_ajaxOperationRelease(string dh, string tm, string file)
        {
            var user = SGArchiveSystem7x.App_Code.CSession.Get_User(Session);
            SGArchiveSystem7xDB.T_ArchiveAj en = new SGArchiveSystem7xDB.T_ArchiveAj()
            {
                DH = dh,
                TM = tm,
                ARID = 4,
                GDBM = (int)user.U_DEPID,
                DZWJ = file,
                SJJLR = user.U_NAME,
                SJJLRQ = DateTime.Now,
                WJLX = 10,
                MJ=6,
                BGQX = 1,
                SJJLLX="用印",
                KFZT =1,
                AJZT=0,
                I05=0

            };
            return SGArchiveSystem7xDB.Linq.T_OperationDal.Release_Operation(en).ToString();
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public string Get_ajaxOperationModify(int id, string dh, string tm, int dep, string gtime, int status)
        {
            var user = SGArchiveSystem7x.App_Code.CSession.Get_User(Session);
            DateTime st = DateTime.Parse(gtime);
            SGArchiveSystem7xDB.T_YYFILE en = new SGArchiveSystem7xDB.T_YYFILE()
            {
                YY_ID = id,
                YY_BH = dh,
                YY_TM = tm,
                YY_USERID = user.U_ID,
                YY_USERNAME = user.U_RNAME,
                YY_DEPID = dep,
                YY_DATE = st,
                YY_STATUS = status
            };
            return SGArchiveSystem7xDB.Linq.T_OperationDal.Modify_Operation(en).ToString();
        }

        /// <summary>
        /// 获取当个数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Get_ajaxOperationListId(int id)
        {
            var en = SGArchiveSystem7xDB.Linq.T_OperationDal.Get_OperationId(id);
            return Json(en, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 上传pdf
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public string UploadPdf(int id)
        {
            try
            {
                var file = Request.Files["file1"];
                string filename = DateTime.Now.Ticks.ToString()+".pdf";
                var fullPath = Path.Combine(StorageRoot, Path.GetFileName(filename));
                file.SaveAs(fullPath);
                //添加压缩
                //SGArchiveSystem7xCommons.SGZip.CompressFile(fullPath, Server.MapPath("~/Content/PDF/" + filename + ".sg"));
                //FileInfo file1 = new FileInfo(fullPath);
                //file1.Delete();
                SGArchiveSystem7xDB.V_User user = SGArchiveSystem7x.App_Code.CSession.Get_User(Session);
                SGArchiveSystem7xDB.Linq.T_OperationDal.Upload(id, filename + ".pdf", user);
                return "1";
            }
            catch (Exception ex)
            {
                return "0";
            }
        }

        private string StorageRoot
        {
            get { return Path.Combine(Server.MapPath("~/Content/PDF")); }
        }

        public string Get_ajaxOperationDel(int id)
        {
            return SGArchiveSystem7xDB.Linq.T_OperationDal.Del_Operation(id).ToString();
        }
    }
}